package net.miranti;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.servlet.ServletContextEvent;

import net.miranti.dao.GenericDao;
import net.miranti.dao.JpaDao;
import net.miranti.dao.ResourceLocalDaoUtils;
import net.miranti.dao.ResourceLocalDaoUtils.OpenPersistenceManagerInViewFilter;

import com.google.gwt.dev.util.collect.HashMap;
import com.google.inject.AbstractModule;
import com.google.inject.Provider;
import com.google.inject.servlet.ServletModule;

public class MirantiGuiceConfig extends DevGuiceConfig {
	@Override
	public void contextInitialized(ServletContextEvent e) {
		super.contextInitialized(e);
		ResourceLocalDaoUtils.start();
	}

	@Override
	public void contextDestroyed(ServletContextEvent e) {
		super.contextDestroyed(e);
		ResourceLocalDaoUtils.stop();
	}

	protected @Override AbstractModule[] getAppModules() {
		return new AbstractModule[] {
				new ServletModule() {
					@Override
					protected void configureServlets() {
						filter("*.rpc").through(OpenPersistenceManagerInViewFilter.class);
						
						Map params = new HashMap();
						params.put("file-repository-context", "meuctm");
					}
				},
				new AbstractModule() {
					@Override
					protected void configure() {
						bind(EntityManager.class).toProvider(EntityManagerProvider.class);
						bind(GenericDao.class).to(JpaDao.class);
					}
				},
		};
	}
	
	public static class EntityManagerProvider implements Provider<EntityManager> {
		@Override
		public EntityManager get() {
			return ResourceLocalDaoUtils.getEntityManager();
		}
	}
}